package com.zuipin.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.SysUnit;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.ISysUnitService;
import com.zuipin.util.Pagination;
import com.zuipin.vo.UnitTreeNode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 组织部门Controller
 * 
 * @ClassName: SysUnitController
 * @Description: TODO
 * @author robot
 * @date 2017年09月147日 09:21:09
 */
@Api(value = "组织部门", description = "组织部门")
@RestController
@RequestMapping(value = "/sysUnit")
public class SysUnitController {
	@Resource
	public ISysUnitService sysUnitService;
	
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	@ApiOperation(value = "根据主键查询组织部门", notes = "查询组织部门")
	public Result<SysUnit> getSysUnitById(@ApiParam(required = true, name = "id", value = "主键 id自增长") @RequestParam Integer id) {
		SysUnit sysUnit = sysUnitService.findById(id);
		Result<SysUnit> result = new Result<SysUnit>();
		if (sysUnit != null) {
			result.setData(sysUnit);
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setErrorCode(ErrorCode.ELSE);
		}
		return result;
	}
	
	@RequestMapping(value = "/getSysUnitInfo", method = RequestMethod.GET)
	@ApiOperation(value = "zhuangjiajian-组织部门详情", notes = "组织部门详情")
	public Result<SysUnit> getSysUnitInfo(@ApiParam(required = true, name = "id", value = "主键 id自增长") @RequestParam Integer id) {
		SysUnit sysUnit = sysUnitService.findById(id);
		SysUnit parentUnit = sysUnitService.findById(sysUnit.getParentId());
		if (parentUnit != null) {
			sysUnit.setParentUnitName(parentUnit.getUnitName());
		}
		Result<SysUnit> result = new Result<SysUnit>();
		result.setData(sysUnit);
		result.setErrorCode(ErrorCode.SUCCESS);
		
		return result;
	}
	
	@RequestMapping(value = "/saveOrUpdateUnit", method = RequestMethod.POST)
	@ApiOperation(value = "新增/修改 组织部门-chenjunjie", notes = "新增/修改组织部门")
	@Valids({
			
			@Valid(name = "sysUnit.unitName", maxLength = 150), @Valid(name = "sysUnit.unitNo", maxLength = 50), @Valid(name = "sysUnit.parentId", maxLength = 10),
			@Valid(name = "sysUnit.sort", maxLength = 10), @Valid(name = "sysUnit.remark", maxLength = 500), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysUnit.id", value = "主键 id自增长", required = true, dataType = "id", paramType = "query"),
			@ApiImplicitParam(name = "sysUnit.unitName", value = "组织名称", required = true, dataType = "unitName", paramType = "query"),
			@ApiImplicitParam(name = "sysUnit.unitNo", value = "组织编号", required = true, dataType = "unitNo", paramType = "query"),
			@ApiImplicitParam(name = "sysUnit.parentId", value = "上级组织ID", dataType = "parentId", paramType = "query"),
			@ApiImplicitParam(name = "sysUnit.sort", value = "排序号", required = false, dataType = "sort", paramType = "query"),
			@ApiImplicitParam(name = "sysUnit.remark", value = "描述/备注", dataType = "remark", paramType = "query"),
			@ApiImplicitParam(name = "sysUnit.updateId", value = "操作人", dataType = "createId", paramType = "query") })
	public Result<SysUnit> saveOrUpdateUnit(@Inject @ApiIgnore SysUnit sysUnit) {
		Result<SysUnit> result = new Result<SysUnit>();
		sysUnitService.saveOrUpdateUnit(sysUnit);
		result.setData(sysUnit);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/delSysUnit", method = RequestMethod.DELETE)
	@ApiOperation(value = "根据主键删除组织部门", notes = "删除组织部门")
	public Result<SysUnit> delSysUnit(Integer id) {
		Result<SysUnit> result = new Result<SysUnit>();
		SysUnit delSysUnit = new SysUnit();
		delSysUnit.setId(id);
		delSysUnit.setDelFlag(SysUnit.DEL_YES);
		sysUnitService.update(delSysUnit);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/searchSysUnit", method = RequestMethod.POST)
	@ApiOperation(value = "搜索组织部门SKU组织部门并分页-zhuangjiajian", notes = "搜索组织部门并分页(名称 编号 描述 状态 )")
	@Valids({
			
			@Valid(name = "sysUnit.unitName", maxLength = 150), @Valid(name = "sysUnit.unitNo", maxLength = 50), @Valid(name = "sysUnit.parentId", maxLength = 10),
			@Valid(name = "sysUnit.delFlag", maxLength = 3), @Valid(name = "sysUnit.unitStatus", maxLength = 3), @Valid(name = "sysUnit.sort", maxLength = 10),
			@Valid(name = "sysUnit.remark", maxLength = 500), @Valid(name = "sysUnit.createId", maxLength = 10), @Valid(name = "sysUnit.createTime", maxLength = 19),
			@Valid(name = "sysUnit.updateId", maxLength = 10), @Valid(name = "sysUnit.updateTime", maxLength = 19), @Valid(name = "page.pageNumber", maxValue = 999),
			@Valid(name = "page.pageSize", maxValue = 999), })
	@ApiImplicitParams({
			
			@ApiImplicitParam(name = "sysUnit.unitName", value = "组织名称", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysUnit.unitNo", value = "组织编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysUnit.id", value = "组织ID(查询该组织下所有的信息 包括其子孙....)", dataType = "integer", required = true, paramType = "query"),
			@ApiImplicitParam(name = "sysUnit.unitStatus", value = "状态 0:未冰结 1已冻结", dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "sysUnit.remark", value = "描述/备注", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), })
	public Result<List<SysUnit>> searchSysUnit(@Inject @ApiIgnore SysUnit sysUnit, @Inject @ApiIgnore Pagination page) {
		PageResult<List<SysUnit>> result = new PageResult<List<SysUnit>>();
		List<SysUnit> list = sysUnitService.searchSysUnit(sysUnit, page);
		
		// list.stream().map(s -> {s.setParentUnitName(sysUnitService.findById(s.getParentId()).getParentUnitName());return s}); GG
		
		// 填充上级组织名称
		for (SysUnit s : list) {
			SysUnit pSysUnit = sysUnitService.findById(s.getParentId());
			if (pSysUnit != null) {
				s.setParentUnitName(pSysUnit.getUnitName());
			}
		}
		
		result.setData(list);
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	/**
	 * 加载树形 组织结构
	 * @param unitStatus
	 * @return
	 */
	@RequestMapping(value = "/getSysUnitTree", method = RequestMethod.POST)
	@ApiOperation(notes = "查询组织树列表", value = "查询组织树列表-zhuangjiajian")
	@Valids({ @Valid(name = "unitStatus", regex = "0|1", required = false) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "unitStatus", value = "0禁用 1启用    组织的状态", required = false, dataType = "integer", paramType = "query") })
	public Result<List<UnitTreeNode>> getSysUnitTree(@RequestParam(required = false) Integer unitStatus) {
		Result<List<UnitTreeNode>> result = new Result<List<UnitTreeNode>>();
		result.setData(sysUnitService.getSysUnitTree(unitStatus));
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
}
